#include "psettingsdialog.h"

PSettingsDialog::PSettingsDialog(QWidget *parent) :
	QDialog(parent),
	m_ui(new Ui::PSettingsDialog)
	{
		m_ui->setupUi(this);
		settings = new QSettings(QDir::toNativeSeparators(QDir::homePath()+"/.picKel/pickel.cfg"), QSettings::IniFormat, this);

		connect(m_ui->buttonBox, SIGNAL(clicked(QAbstractButton*)), this, SLOT(save(QAbstractButton*)));

		syncSettings();
	}

PSettingsDialog::~PSettingsDialog()
	{
		delete m_ui;
	}

void PSettingsDialog::changeEvent(QEvent *e)
	{
		QDialog::changeEvent(e);
		switch (e->type()) {
		case QEvent::LanguageChange:
			m_ui->retranslateUi(this);
			break;
		default:
			break;
		}
	}

QSettings *PSettingsDialog::settingsConf()
	{
		return settings;
	}

void PSettingsDialog::save(QAbstractButton *button)
{
	if (m_ui->buttonBox->buttonRole(button) == QDialogButtonBox::ApplyRole)
	{
		settings->setValue("NotesDisplay/wrapping", m_ui->wrapIcons->isChecked());
		settings->setValue("NotesDisplay/adjustIconSizes", m_ui->adjustSizes->isChecked());

		if (m_ui->topRadio->isChecked())
			settings->setValue("NotesDisplay/listPosition", "top");
		else if (m_ui->leftRadio->isChecked())
			settings->setValue("NotesDisplay/listPosition", "left");
		else if (m_ui->bottomRadio->isChecked())
			settings->setValue("NotesDisplay/listPosition", "bottom");
		else if (m_ui->rightRadio->isChecked())
			settings->setValue("NotesDisplay/listPosition", "right");

		if (m_ui->tabbedRadio->isChecked())
			settings->setValue("NoteEditor/windowViewMode", "tabbed");
		else if (m_ui->subwindowRadio->isChecked())
			settings->setValue("NoteEditor/windowViewMode", "subwindow");

		emit updateGUI();
	}

	else if (m_ui->buttonBox->buttonRole(button) == QDialogButtonBox::ResetRole)
		syncSettings();
}

void PSettingsDialog::syncSettings()
{
	m_ui->wrapIcons->setChecked(settings->value("NotesDisplay/wrapping", false).toBool());
	m_ui->adjustSizes->setChecked(settings->value("NotesDisplay/adjustIconSizes", false).toBool());

	if (settings->value("NotesDisplay/listPosition", "top") == "top")
		m_ui->topRadio->setChecked(true);
	else if (settings->value("NotesDisplay/listPosition", "top") == "left")
		m_ui->leftRadio->setChecked(true);
	else if (settings->value("NotesDisplay/listPosition", "top") == "bottom")
		m_ui->bottomRadio->setChecked(true);
	else if (settings->value("NotesDisplay/listPosition", "top") == "right")
		m_ui->rightRadio->setChecked(true);

	if (settings->value("NoteEditor/windowViewMode", "tabbed") == "tabbed")
		m_ui->tabbedRadio->setChecked(true);
	else if (settings->value("NoteEditor/windowViewMode", "tabbed") == "subwindow")
		m_ui->subwindowRadio->setChecked(true);
}
